<template>
  <div class="login-container">
    <el-form ref="loginForm"
             class="login-form"
             autocomplete="on"
             :model="loginForm"
             :rules="loginRules"
             label-position="left">
      <div class="title-container">
        <h3 class="title">{{ $t('login.title') }}</h3>
        <i class="iconfont"
           :class="loginIcon"
           data-spm-anchor-id="a212t0.3047821.0.i0.101079f40k4eBi"
           @click="changeMethod"></i>
        <div class="login-tip">
          <div class="poptip">
            <div class="poptip-arrow">
              <em data-spm-anchor-id="a212t0.3047821.0.i1.101079f40k4eBi"></em>
              <span></span>
            </div>
            <div class="poptip-content"
                 v-text="loginMethod"></div>
          </div>
        </div>
      </div>
      <template v-if="loginMethod === '扫码登录'">
        <el-form-item prop="username">
          <div class="login_input">
            <span class="svg-container svg-container_login">
              <svg-icon icon-class="user" />
            </span>
            <el-input v-model="loginForm.username"
                      name="username"
                      type="text"
                      :placeholder="$t('login.username')" />
          </div>
        </el-form-item>

        <el-form-item prop="password">
          <div class="login_input">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input v-model="loginForm.password"
                      name="password"
                      :type="passwordType"
                      :placeholder="$t('login.password')"
                      @keyup.enter.native="handleLogin" />
            <span class="show-pwd"
                  @click="showPwd">
              <svg-icon icon-class="eye" />
            </span>
          </div>
        </el-form-item>

        <el-form-item style="border:none;background:none"
                      prop="type">
          <el-radio-group v-model="loginForm.type">
            <el-radio style="color:#fff"
                      :label="1">管理员</el-radio>
            <el-radio style="color:#fff"
                      :label="2">广告主</el-radio>
            <el-radio style="color:#fff"
                      :label="3">商家</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-button type="primary"
                   style="width:100%;margin-bottom:30px;"
                   :loading="loading"
                   @click.native.prevent="handleLogin">{{ $t('login.logIn') }}</el-button>
      </template>
      <template v-else>
        <div style="width:100%;height:100%;text-align:center;position: relative;">
          <img :src="QRCode" />
          <div class="codeTips">打开微信扫描码二维码</div>
          <div v-if="expired"
               class="qrcode-error">
            <p>二维码已失效</p>
            <button class="refresh"
                    type="button"
                    @click="changeQRCode">刷新二维码</button>
          </div>
        </div>
      </template>
    </el-form>
  </div>
</template>

<script>
var QRCode = require("qrcode");
// import QRCode from "qrcodejs2"; // 引入qrcode
// import axios from "axios";

export default {
  name: "Login",
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码长度必须大于六位数"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: "",
        type: 1
      },
      QRCode: "",
      loginMethod: "扫码登录",
      loginIcon: "icon-qrcode",
      expired: false,
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword }
        ]
      },
      passwordType: "password",
      loading: false
    };
  },
  methods: {
    changeMethod() {
      if (this.loginMethod === "扫码登录") {
        this.loginMethod = "密码登录";
        this.loginIcon = "icon-password";
        this.showQRCode();
      } else {
        this.loginMethod = "扫码登录";
        this.loginIcon = "icon-qrcode";
      }
    },
    changeQRCode() {
      this.showQRCode();
      this.expired = false;
    },
    showQRCode() {
      const getCodePath = {
        company: "STQ",
        api: "pc_wx_getCode",
        version: "1.0",
        data: {}
      };
      this.socketApi.sendSock(JSON.stringify(getCodePath), res => {
        if (res) {
          this.sendSock(res);
        }
      });
    },
    getQRCode(weChatUrl) {
      let QRurl = "";
      if (weChatUrl) {
        QRCode.toDataURL(weChatUrl, function(err, url) {
          if (err) {
            QRurl = "";
          } else {
            QRurl = url;
          }
        });
      } else {
        QRurl = "";
      }
      this.QRCode = QRurl;
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          const homeLoginPath = {
            company: "STQ",
            api: "pc_home_login",
            version: "1.0",
            data: {
              username: this.loginForm.username,
              password: this.loginForm.password,
              type: this.loginForm.type
            }
          };
          this.socketApi.sendSock(JSON.stringify(homeLoginPath), res => {
            if (res) {
              this.sendSock(res);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    sendSock(res) {
      if (res && res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
          // this.$router.push({ path: "/login" });
          this.loading = false;
        } else {
          if (resj.api === "pc_home_login") {
            // resj.data["type"] = this.loginForm.type; // 添加登录类型
            this.$store
              .dispatch("LoginByUsername", resj.data)
              .then(() => {
                this.$router.push({ path: "/index/index" });
              })
              .catch(() => {
                this.loading = false;
              });
          } else if (resj.api === "pc_wx_getCode") {
            this.getQRCode(resj.data);
            this.timer = setTimeout(() => {
              this.expired = true;
            }, 300000);
          } else if (resj.api === "pc_wx_checkWebLogin") {
            this.$store
              .dispatch("LoginByUsername", resj.data)
              .then(() => {
                this.$router.push({ path: "/index/index" });
              })
              .catch(() => {
                this.loading = false;
              });
          }
        }
      }
    }
  }
};
</script>
<style scoped>
@import "../../assets/css/iconfont.css";
.title-container .iconfont {
  display: block;
  cursor: pointer;
  position: absolute;
  top: 0;
  right: 0;
  font-size: 40px;
  color: #409eff;
}
.title-container .login-tip {
  position: absolute;
  top: 0;
  right: 45px;
  display: block;
}
.title-container .poptip {
  line-height: 16px;
  position: relative;
  z-index: 9999;
  background-color: #66b1ff;
  border-color: #3a8ee6;
  border: 1px solid #1c7ce0;
  padding: 5px 10px;
  color: #f5f7fa;
}
.title-container .poptip .poptip-arrow em,
.title-container .poptip .poptip-arrow span {
  position: absolute;
  *zoom: 1;
  width: 0;
  height: 0;
  border-color: hsla(0, 0%, 100%, 0);
  border-color: transparent \0;
  *border-color: transparent;
  _border-color: tomato;
  _-webkit-filter: chroma(color=tomato);
  _: chroma(color=tomato);
  border-style: solid;
  overflow: hidden;
  top: 0;
  left: 0;
  border-width: 6px 0 6px 6px;
  border-left-color: #66b1ff;
}
.title-container .poptip .poptip-arrow {
  position: absolute;
  z-index: 10;
  *zoom: 1;
  top: 8px;
  right: 0;
}
.title-container .poptip .poptip-arrow em {
  top: 0;
  left: 1px;
  border-left-color: #2d3a4b;
  border-width: 6px 0 6px 6px;
}
.login-container .qrcode-error {
  background: hsla(0, 0%, 100%, 0.95);
  position: absolute;
  left: 0;
  top: 0;
  z-index: 9999;
  width: 100%;
  height: 100%;
}
.login-container .qrcode-error p {
  margin-top: 25%;
}
.login-container .qrcode-error button {
  border-radius: 0;
  background: #00c1de;
  border-color: #00c1de;
  width: 110px;
  line-height: 34px;
  text-align: center;
  margin: 0 auto;
  display: block;
  color: #fff;
  border-radius: 3px;
  font-size: 14px;
  cursor: pointer;
}
</style>
<style rel="stylesheet/scss" lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #eee;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
    &::first-line {
      color: $light_gray;
    }
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  // .el-form-item {
  //   border: 1px solid rgba(255, 255, 255, 0.1);
  //   background: rgba(0, 0, 0, 0.1);
  //   border-radius: 5px;
  //   color: #454545;
  // }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 520px;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      font-weight: bold;
    }
    .set-language {
      color: #fff;
      position: absolute;
      top: 5px;
      right: 0px;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 35px;
    bottom: 0px;
  }
  .login_input {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  .codeTips {
    margin-top: 12px;
    color: #ccc;
    font-size: 1.1rem;
  }
}
</style>
